
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.ResourceBundle;
import java.util.Scanner;
import java.util.Set;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Node;
import nu.xom.ParsingException;
import nu.xom.Text;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


public class SWTHelloWorld  {

	//this stores all kinds of predefined styles
	static HashMap<String,HashMap<String, Integer>> styleSet = new HashMap<String, HashMap<String, Integer>>();
	//this maps the element to the style
	static HashMap<String, String> semMapping = new HashMap<String,String>();
	static StyledText view;
	static Display display;

	public static void main (String [] args) {
		display = new Display ();
		Shell shell = new Shell(display);
		FormLayout gd = new FormLayout();
		shell.setLayout(gd);

		view = new StyledText (shell, SWT.BORDER | SWT.H_SCROLL 
				| SWT.V_SCROLL | SWT.WRAP);
		FormData location = new FormData();
		location.left = new FormAttachment(0);
		location.right = new FormAttachment(50);
		location.top = new FormAttachment (0);
		location.bottom = new FormAttachment(50);
		view.setLayoutData (location);
		shell.open();
		readCfg("");//set up the table
		openDocument("myXML.xml");
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		display.dispose ();
	}

	static void openDocument (String fileName) {
		Builder parser = new Builder();
		Document doc;
		try {
			doc = parser.build (fileName);
		} catch (ParsingException e) {
			System.out.println("Malformed document");
			return;
		}
		catch (IOException e) {
			System.out.println("Could not open " + fileName);
			return;
		}

		Element rootElement = doc.getRootElement();//this needs to be final, because it will be used by a different thread        
		walkTree (rootElement);
	}

	private void readSem(String fileName){
		fileName = "sem";
		ResourceBundle bundle = ResourceBundle.getBundle(fileName);
		Enumeration bundleKeys = bundle.getKeys();
		while(bundleKeys.hasMoreElements()){
			String key = (String)bundleKeys.nextElement();
			String value = bundle.getString(key);
			System.out.println("key = " + key + ", " + "value = " + value);
			semMapping.put(key, value);
		}
	}
	private static void readCfg(String fileName){
		fileName = "cfg";
		ResourceBundle labels = ResourceBundle.getBundle(fileName);
		Enumeration bundleKeys = labels.getKeys();
		while (bundleKeys.hasMoreElements()) {
			String key = (String)bundleKeys.nextElement();
			String value = labels.getString(key);
			System.out.println("key = " + key + ", " + "value = " + value);
		}
		ArrayList<String> styleNames = splitIntoArray(labels.getString("style"));
		for(String s: styleNames){
			System.out.println("Style " +s);
			readStyle(s);
		}
	}
	private static ArrayList<String> splitIntoArray(String s){
		ArrayList<String> strings = new ArrayList<String>();
		Scanner src = new Scanner(s);
		// Set delimiters to space and comma.
		// ", *" tells Scanner to match a comma and zero or more spaces as
		// delimiters.
		src.useDelimiter(", *");
		// Read and sum numbers.
		while (src.hasNext()) {
			strings.add(src.next());
		}
		return strings;
	}


	private static void readStyle(String styleName){
		styleName = "style_"+ styleName;
		HashMap<String, Integer> myStyle = new HashMap<String, Integer>();
		ResourceBundle labels = ResourceBundle.getBundle(styleName);
		Enumeration bundleKeys = labels.getKeys();
		while (bundleKeys.hasMoreElements()) {
			String key = (String)bundleKeys.nextElement();
			Integer value = Integer.parseInt(labels.getString(key));
			myStyle.put(key, value);
			System.out.println("key = " + key + ", " + "value = " + value);
		}
		styleSet.put(styleName, myStyle);
	}


	static int size =0;
	private static void walkTree (Node node) {
		Node newNode;
		for (int i = 0; i < node.getChildCount(); i++) {
			newNode = node.getChild(i);
			if (newNode instanceof Element) {
				//if(newNode.getChildCount()>1)
					walkTree (newNode);
				//else{
					System.out.println(newNode.toString());
					setStyle(newNode);
				//}
			}
			else if (newNode instanceof Text) {
				final String value = newNode.getValue();
				view.append(value);
//				StyleRange styleRange = new StyleRange();
//				styleRange.fontStyle = SWT.BOLD;
//				styleRange.foreground = new Color(display, 255, 127, 0);;
//				view.setStyleRange(styleRange);

				FontData[] fD = view.getFont().getFontData();
				fD[0].setHeight(16);
				view.setFont( new Font(display,fD[0]));
			}
		}
	}
	private static void setStyle(Node newNode){
		//System.out.println(newNode.getValue());
		//String styleName = semMapping.get(newNode.getValue());
		//System.out.println(styleName);
		//HashMap<String,Integer> myStyle = styleSet.get(styleName);
		//System.out.println(myStyle.toString());
//		newNode.addAttribute(arg0)
	}
}